package redora.rdo.client.model;

import com.smartgwt.client.types.Side;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.Label;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.grid.ListGridRecord;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.tab.Tab;
import com.smartgwt.client.widgets.tab.TabSet;

public class AttributesListCanvas extends Canvas {

	TabSet topTabSet = new TabSet();

	Tab attTab = new Tab("Attributes");

	// ListGrid fieldList = new ListGrid();

	public AttributesListCanvas(final String objectName) {
		// the right panel of tab to show
		topTabSet.setTabBarPosition(Side.TOP);
		topTabSet.setTabBarAlign(Side.LEFT);
		topTabSet.setWidth100();
		topTabSet.setHeight100();

		HLayout mainPanel = new HLayout();
		mainPanel.setHeight100();
		mainPanel.setWidth100();

		// the first tab for each model
		topTabSet.addTab(attTab);

		ListGrid fieldList = new ListGrid() {
			protected Canvas getExpansionComponent(final ListGridRecord record) {
				if (record.getAttributeAsString("Name").equalsIgnoreCase(
						"Attributes")) {
					ListGrid grid = new ListGrid();
					grid.setWidth(1000);
					grid.setHeight(250);
					grid.setCellHeight(22);
					FieldDataSource ds = FieldDataSource.getInstance();
					ds.setTitle(objectName);
					grid.setDataSource(ds);
					grid.setAutoFetchData(true);
					return grid;
				} else if (record.getAttributeAsString("Name")
						.equalsIgnoreCase("Description")) {
					Label label = new Label("");
					return label;
				} else {
					return null;
				}
			}
		};

		ListGridRecord attrecord = new ListGridRecord();
		attrecord.setAttribute("Name", "Attributes");

		ListGridRecord desRecord = new ListGridRecord();
		desRecord.setAttribute("Name", "Description");

		ListGridRecord brRulesRecord = new ListGridRecord();
		brRulesRecord.setAttribute("Name", "BusinessRules");

		ListGridRecord[] records = { attrecord, desRecord, brRulesRecord };

		// gridlist for model attributes

		fieldList.setWidth100();
		fieldList.setHeight100();
		// FieldDataSource ds = FieldDataSource.getInstance();
		// ds.setTitle(objectName);
		// fieldList.setDataSource(ds);
		// fieldList.fetchData();
		fieldList.setCanExpandRecords(true);
		ListGridField field = new ListGridField("Name", "Name");
		fieldList.setFields(field);
		fieldList.setData(records);

		mainPanel.addChild(fieldList);
		attTab.setPane(mainPanel);
		this.setWidth100();
		this.setHeight100();
		this.addChild(topTabSet);
	}

}
